import request from '../request';

export const tokenStorageName = 'upload-token';

export const getToken = () => {
    const tokenInfo = sessionStorage.getItem(tokenStorageName);
    if (tokenInfo) {
      const info = JSON.parse(tokenInfo);
      if (Date.now() < Number(info.startTime)) {
        return Promise.resolve(info.token);
      }
    }
    return request.get(`/api/token?startTime=${Date.now()}`).then((data) => {
      sessionStorage.setItem(tokenStorageName, JSON.stringify(data));
      return data.token;
    });
};

export const isDev = process.env.NODE_ENV === 'development';
